package com.levy.contronller.common;

import java.util.*;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import com.levy.contronller.javaWeb.threadConcurrence.MultiThreadProxyHubApiTest;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.levy.model.User;
import com.levy.service.UserService;
import com.levy.utils.exceptionBean.UniqueDaoException;

@Controller
public class WebTestContronller extends ExceptionController {
	
	@Autowired
	private UserService userService;
	
	@RequestMapping("/timerTest")
	public Map<String, Object> timerTest(HttpServletRequest request){
		Map<String, Object> map = new HashMap<String, Object>();
//		String aa = request.getParameter("aa");
//		request.getSession().setAttribute("dd",aa);

//		final String time = DateTools.getCurrentDateByFormat("HHmmss");
		final String time = "HHmmss";

		Timer timer = new Timer();
		timer.schedule(new TimerTask() {
//			String time = DateTools.getCurrentDateByFormat("HHmmss");
			int i = 0;
			@Override
			public void run() {
//				String time = DateTools.getCurrentDateByFormat("HHmmss");
				System.out.println(time);
				User user = userService.getUserByCreateTime(time);
				if (user!=null) {
					System.out.println("第"+(++i)+"次查询，已找到数据"+user.toString());
					this.cancel();
				}else{
					System.out.println("第"+(++i)+"次查询，没有数据");
				}
			}
		}, 500, 1000);
		return map;
	}
	
	
	@RequestMapping("/threadTest")
	public Map<String, Object> threadTest(){
		Map<String, Object> map = new HashMap<String, Object>();
		Executor excutor =  Executors.newFixedThreadPool(20);
		excutor.execute(new Runnable() {
			@Override
			public void run() {
				int i = 0;
				while(true){
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					User user = userService.getUserById(i);
					if (user!=null) {
						System.out.println("第"+(++i)+"次查询，已找到数据"+user.toString());
						break;
					}else{
						System.out.println("第"+(++i)+"次查询，没有数据");
					}
				}
				
			}
		});
		return map;
	}

	
	@RequestMapping("/exceptionTest")
	@ResponseBody
	public Map<String, Object> exceptionTest(){
		Map<String, Object> map = new HashMap<String, Object>();
				
		try {
			int count = 10/0;
			List list = new ArrayList();
			Iterator iterator = list.iterator();
			if (iterator.hasNext()){
				System.out.println(iterator.next());
			}
		} catch (Exception e) {
			System.out.println(e.getMessage());
			throw new UniqueDaoException("数据不唯一");
//			throw new UniqueDaoException();
		}
		return map;
	}


	@RequestMapping(value = "/threadConcurrenceTest")
	@ResponseBody
	public  Map<String, Object> threadConcurrenceTest(HttpServletRequest request,HttpServletResponse response) throws Exception{
		Map<String, Object> map = new HashMap<String, Object>();
		if (StringUtils.isBlank(request.getParameter("pro"))) {
			map.put("data", "参数不全");
			return map;
		}
		// 总访问量和并发量两重循环，依次增大访问
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		for (int j = 500; j < 600; j += 100) {
			for (int i = 10; i < 100; i += 10) {
				// 要测试的URL
				String url = "http://10.253.43.7:8080/app/page/appIndex/index.html";
				Map<String, Object> map1 = new MultiThreadProxyHubApiTest(i, j, url, "").run();
				list.add(map1);
			}
		}
		System.out.println("finished!");
		map.put("data", list);
		return map;
	}

}
